package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.MesProduceLineRpcService;
import com.hunttown.mes.rpc.domain.MesProduceLineDTO;
import com.hunttown.mes.rpc.domain.query.MesProduceLineDTOQuery;
import com.hunttown.mes.service.MesProduceLineService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.MesProduceLineConverts.convert;

/**
 * created by wangjunfu on 2022-03-30 15:27:55
 */
@Service
public class MesProduceLineRpcServiceImpl implements MesProduceLineRpcService {

    private final Logger logger = LoggerFactory.getLogger(MesProduceLineRpcServiceImpl.class);

    private final MesProduceLineService mesProduceLineService;

    @Autowired
    public MesProduceLineRpcServiceImpl(MesProduceLineService mesProduceLineService) {
        this.mesProduceLineService = mesProduceLineService;
    }

    @Override
    public MesProduceLineDTO insert(MesProduceLineDTO mesProduceLineDTO) {
        try {
            return convert(mesProduceLineService.insert(convert(mesProduceLineDTO)));
        } catch (Exception e) {
            logger.error("MesProduceLine create failed.", e);
            throw new RuntimeException("MesProduceLine create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(MesProduceLineDTO mesProduceLineDTO) {
        try {
            return mesProduceLineService.updateInfoById(convert(mesProduceLineDTO));
        } catch (Exception e) {
            logger.error("MesProduceLine update by id failed.", e);
            throw new RuntimeException("MesProduceLine update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(MesProduceLineDTOQuery mesProduceLineDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesProduceLineDTOQuery.getW_id() != null && mesProduceLineDTOQuery.getW_id() > 0) {
                query.put("w_id", mesProduceLineDTOQuery.getW_id());
            }

            if (mesProduceLineDTOQuery.getIds() != null && !mesProduceLineDTOQuery.getIds().equals("")) {
                query.put("ids", mesProduceLineDTOQuery.getIds());
            }

            if (mesProduceLineDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesProduceLineDTOQuery.getStopFlag());
            }

            if (mesProduceLineDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesProduceLineDTOQuery.getDeleteFlag());
            }

            return mesProduceLineService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("MesProduceLine update by query failed.", e);
            throw new RuntimeException("MesProduceLine update by query failed, Please order your code.", e);
        }
    }

    @Override
    public MesProduceLineDTO selectById(Integer id) {
        try {
            return convert(mesProduceLineService.selectById(id));
        } catch (Exception e) {
            logger.error("MesProduceLine select by id failed.", e);
            throw new RuntimeException("MesProduceLine select by id failed, Please order your code.", e);
        }
    }

    @Override
    public MesProduceLineDTO selectObjByQuery(MesProduceLineDTOQuery mesProduceLineDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesProduceLineDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesProduceLineDTOQuery.getStopFlag());
            }

            if (mesProduceLineDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesProduceLineDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(mesProduceLineDTOQuery.getOrderBy())) {
                query.put("orderBy", mesProduceLineDTOQuery.getOrderBy());
            }

            return convert(mesProduceLineService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("MesProduceLine select obj by query failed.", e);
            throw new RuntimeException("MesProduceLine select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<MesProduceLineDTO> getListForPage(MesProduceLineDTOQuery mesProduceLineDTOQuery) {
        Query query = Query.page(mesProduceLineDTOQuery.getPn(), mesProduceLineDTOQuery.getPageSize());

        if (mesProduceLineDTOQuery.getId() != null && mesProduceLineDTOQuery.getId() > 0) {
            query.put("id", mesProduceLineDTOQuery.getId());
        }

        if (mesProduceLineDTOQuery.getIds() != null && !mesProduceLineDTOQuery.getIds().equals("")) {
            query.put("ids", mesProduceLineDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(mesProduceLineDTOQuery.getLikeName())) {
            query.put("likeName", mesProduceLineDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(mesProduceLineDTOQuery.getShowDate())) {
            query.put("showDate", mesProduceLineDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(mesProduceLineDTOQuery.getStartDate())) {
            query.put("startDate", mesProduceLineDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(mesProduceLineDTOQuery.getEndDate())) {
            query.put("endDate", mesProduceLineDTOQuery.getEndDate());
        }

        if (mesProduceLineDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", mesProduceLineDTOQuery.getStopFlag());
        }

        if (mesProduceLineDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", mesProduceLineDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(mesProduceLineDTOQuery.getOrderBy())) {
            query.put("orderBy", mesProduceLineDTOQuery.getOrderBy());
        }

        List<MesProduceLineDTO> list = convert(mesProduceLineService.selectByQuery(query));
        Page<MesProduceLineDTO> page = Page.from(mesProduceLineDTOQuery.getPn(), mesProduceLineDTOQuery.getPageSize(), list);
        page.setTotalItems(mesProduceLineService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return mesProduceLineService.deleteById(id);
        } catch (Exception e) {
            logger.error("MesProduceLine delete failed.", e);
            throw new RuntimeException("MesProduceLine delete failed, Please order your code.", e);
        }
    }
}